package vcp.gui.forms;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JOptionPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import vcp.gui.components.VCPPanel;

import vcp.gui.forms.validators.NumericValidator;

import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import control.OrderController;
import control.SimpleClient;

import db.parking.beans.Orders;

import java.awt.Color;


public class CancelOrderForm extends VCPPanel {
	private JTextField textOrderID;
	private JButton btnCancelOrder;
	private JLabel lblCancellOrderError;
	private SimpleClient client;

	/**
	 * Create the panel.
	 */
	public CancelOrderForm(SimpleClient client) {
		this.client = client;
		lblCancellOrderError = new JLabel("");
		lblCancellOrderError.setForeground(Color.RED);
		JLabel lblOrderID = new JLabel("Order ID");
		textOrderID = new JTextField();
		textOrderID.setColumns(10);

		btnCancelOrder = new JButton("cancellation");
		btnCancelOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionPerformedHandle(e);
			}
		});

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				lblOrderID)
																		.addGap(18)
																		.addComponent(
																				textOrderID,
																				GroupLayout.PREFERRED_SIZE,
																				257,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnCancelOrder))
														.addComponent(
																lblCancellOrderError))
										.addContainerGap(28, Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGap(10)
										.addComponent(lblCancellOrderError)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																textOrderID,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblOrderID)
														.addComponent(
																btnCancelOrder))
										.addContainerGap(245, Short.MAX_VALUE)));
		setLayout(groupLayout);
	}

	private void actionPerformedHandle(ActionEvent e) {
		
		String ID = textOrderID.getText();// get the text box from gui
		Orders bean; // order to send
		OrderController orderControl; // cretae order controller
		NumericValidator verifyID = new NumericValidator(); // create validator
															// to cheack the id

		if (!verifyID.validate(ID)) { // wrong input
			JOptionPane.showMessageDialog(null,
					"Invalid ID Entered,Please Re-Enter",
					"Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		} else { // good input
			try {
				orderControl = new OrderController(client);
				bean = new Orders();
				bean.setOid(Integer.parseInt(ID));
				System.out.println("order_created");
				orderControl.CancelOrder(bean);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
}